<?php
/**
 * Created by PhpStorm.
 * User: woody
 * Date: 17-2-16
 * Time: 下午10:46
 */

namespace App\Http\Controllers\Admin;

use App\Models\Users;
use Illuminate\Http\Request;
use App\Models\Dashboard;
use Validator , Config , Session , Cookie;

class UsersController extends AdminController {

    public function index(Request $request){
        $page = $request->get('page', 1);
        $pageSize = $request->get('pageSize', 20);
        $search = [];
        if($request->has('search')){
            $search['search'] = $request->get('search');
        }
        $UsersModel = new Users();
        $data=$UsersModel->Users($search, $page, $pageSize);
        $eatEveryDayPrize = new Dashboard();
        $total=$eatEveryDayPrize->CountUsers();
        return response()->json(['code' => 0, 'msg' => '', 'data' => $data , 'total' => $total]);

        // return view('admin.Goods', $this->_response);
    }
    //修改页面
    public function getUser($id){
        $this->_response['Users'] = Users::where('id', $id)->first();
        $this->_response['_active']['_action']  = 'edit';
        $data=$this->_response;
        return response()->json(['code' => 0, 'msg' => '', 'data' =>$data]);
    }
    public function getUsersUpdate(Request $request){
        $data = $request->all();
        if( $data['member']==='不是'){
            $data['member']=0;
        }else {
            $data['member']=1;
        }
        $saveEdit = [
            'id' => $data['id'],
            'balance' => $data['balance'],
            'mobile' => $data['mobile'],
            'member' => $data['member'],
            'updated_at' => date('Y-m-d H:i:s', time())
        ];
        $UsersModel = new Users();
        if($UsersModel->saveEdit($saveEdit,$data['id'])){
            return response()->json(['code' => 0, 'msg' => '修改成功']);
        }else{
            return response()->json(['code' => -1, 'msg' => '修改失败']);
        }
    }

    public function Delete($id){
        $saveEdit =[
            'deleted_at' => date('Y-m-d H:i:s', time())
        ];
        $UsersModel = new Users();
        if($UsersModel->saveEdit($saveEdit,$id)){
            return response()->json(['code' => 0, 'msg' => '删除成功']);
        }else{
            return response()->json(['code' => -1, 'msg' => '删除失败']);
        }
    }


}